package com.jmz.apt.api.Provider;

import com.jmz.apt.api.entity.dbEntity.JmzAdminRole;
import com.jmz.apt.api.message.request.Condition;
import com.jmz.apt.api.message.request.Query;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * Created by fanglexue on 2019/10/31.
 */
public class AdminRoleProvider extends BaseProvider {

    public String getTableName(){
        return "jmz_admin_role";
    }

    public String saveAdminRole(JmzAdminRole jmzAdminRole){
        SQL sql = new SQL();
        sql.INSERT_INTO(getTableName());
        if (jmzAdminRole.getId() != null) {
            sql.VALUES("id", "#{id}");
        }

        if (jmzAdminRole.getName() != null) {
            sql.VALUES("name", "#{name}");
        }

        return sql.toString();
    }


    public String findUsers(Integer roleId){
        String sql = " ";
        sql =  new SQL() {{
            SELECT("au.*");
            FROM(getTableName() + "  ar ");
            JOIN(" jmz_admin_user_roles  aur  on ar.id =  aur.role_id");
            JOIN(" jmz_admin_user au on aur.admin_id = au.id");
            WHERE("ar.id = #{roleId}");
        }}.toString();
        return sql;
    }

    /**
     * Find resource list with the given role id
     * @param roleId
     * @return
     */
    public String findResources(Integer roleId){
        String sql = " ";
        sql =  new SQL() {{
            SELECT("ar.*");
            FROM(getTableName() + "  aro ");
            JOIN(" jmz_admin_role_resource  arr  on aro.id =  arr.role_id");
            JOIN(" jmz_admin_resource ar on arr.resource_id = ar.id");
            if(!roleId.equals(1)){
                WHERE("aro.id = #{roleId}");
            }
        }}.toString();
        return sql;
    }

    public String getCount(List<Condition> conditions) {
        return super.count(conditions);
    }

    public String select(String columns, List<Condition> conditions, Query query) {
        return  super.findAll(columns ,conditions, query);
    }

    public String deleteUserRole(List<Integer> ids) {
        StringBuilder sb = new StringBuilder();
        sb.append("delete from jmz_admin_user_roles  where role_id  in (");
        for (int i = 0; i < ids.size(); i++) {
            sb.append("'").append(ids.get(i)).append("'");
            if (i < ids.size() - 1)
                sb.append(",");
        }
        sb.append(")");
        return sb.toString();
    }

    public String deleteResourceRole(List<Integer> ids) {
        StringBuilder sb = new StringBuilder();
        sb.append("delete from jmz_admin_role_resource  where role_id  in (");
        for (int i = 0; i < ids.size(); i++) {
            sb.append("'").append(ids.get(i)).append("'");
            if (i < ids.size() - 1)
                sb.append(",");
        }
        sb.append(")");
        return sb.toString();
    }

    public String deleteRole(List<Integer> ids) {
        StringBuilder sb = new StringBuilder();
        sb.append("delete from jmz_admin_role  where id in (");
        for (int i = 0; i < ids.size(); i++) {
            sb.append("'").append(ids.get(i)).append("'");
            if (i < ids.size() - 1)
                sb.append(",");
        }
        sb.append(")");
        return sb.toString();
    }

    public String insertUserRoleBatch(List<Integer> userIds,Integer roleId){
        StringBuilder sb = new StringBuilder();
        sb.append("insert into jmz_admin_user_roles(admin_id,role_id,create_time,update_time) values");
        for (int i = 0; i < userIds.size(); i++) {
            sb.append("( '").append(userIds.get(i)).append("',");
            sb.append("'").append(roleId).append("',");
            sb.append("now(),");
            sb.append("now() )");
            if (i < userIds.size() - 1)
                sb.append(",");
        }
        return sb.toString();
    }

    public String insertResourceRoleBatch(List<Integer> resourceIds,Integer roleId){
        StringBuilder sb = new StringBuilder();
        sb.append("insert into jmz_admin_role_resource(resource_id,role_id,create_time,update_time) values");
        for (int i = 0; i < resourceIds.size(); i++) {
            sb.append("( '").append(resourceIds.get(i)).append("',");
            sb.append("'").append(roleId).append("',");
            sb.append("now(),");
            sb.append("now() )");
            if (i < resourceIds.size() - 1)
                sb.append(",");
        }
        return sb.toString();
    }
}
